package contrallor;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的文件格式的编码格式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //1,获取到请求中的参数,用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if(username==null||password==null||"".equals(username)||"".equals(password)){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前用户名或者密码为空");
            return;
        }
        //2.和数据库中的内容进行对比
        UserDao  userDao = new UserDao();
        User user = userDao.selectByName(username);

        //在数据库中没有查找到用户信息或者用户的密码错误
        if(user==null||!user.getPassword().equals(password)){

            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前用户名或者密码错误");
            return;
        }
        //3.如果比较通过，就会创建会话
        HttpSession session = req.getSession(true);
        //把刚才创建的用户信息,存储到会话中
        session.setAttribute("user",user);
        //4.返回一个重定向报文,跳转到博客列表页
        resp.sendRedirect("blog_list.html");
    }
    //这个方法用来让前端页面检测当前的登录状态

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        HttpSession session = req.getSession(false);
        if(session==null){
            //检测下会话是否存在,如果不存在说明未登录.
            User user=new User();
            resp.getWriter().write(objectMapper.writeValueAsString(user));
            return;
        }
        User user = (User)session.getAttribute("user");
        if(user==null){
            //虽然有会话,但是会话里面没有 user 对象,也视为未登录.
            user=new User();
            resp.getWriter().write(objectMapper.writeValueAsString(user));
            return;
        }
        //已经登录状态!
        //注意,此处不要把密码返回给前端
        user.setPassword("");
        resp.getWriter().write(objectMapper.writeValueAsString(user));

    }
}
